<template>
  <div class="department-container">
    <div class="app-container">
      <el-card>
        <!-- 用一个行列布局 -->
        <el-row
          type="flex"
          justify="space-between"
          align="middle"
          style="height: 40px"
        >
          <el-col :span="20">
            <svg-icon icon-class="naogen" /><span>江苏传智播客教育科技股份有限公司</span>
          </el-col>
          <el-col :span="4">
            <el-row type="flex" justify="end">
              <!-- 两个内容 -->
              <el-col>负责人</el-col>
              <!-- 下拉菜单 element -->
              <el-col>
                <el-dropdown>
                  <span> 操作<i class="el-icon-arrow-down" /> </span>
                  <el-dropdown-menu slot="dropdown">
                    <el-dropdown-item
                      @click.native="hAdd('')"
                    >添加子部门</el-dropdown-item>
                  </el-dropdown-menu>
                </el-dropdown>
              </el-col>
            </el-row>
          </el-col>
        </el-row>

        <!-- 使用el-tree放主体的内容 -->
        <el-tree :data="list">
          <template #default="{ data }">
            <el-row
              type="flex"
              justify="space-between"
              align="middle"
              style="height: 40px; width: 100%"
            >
              <el-col :span="20">
                <svg-icon icon-class="naogen" /><span>{{ data.name }}</span>
              </el-col>
              <el-col :span="4">
                <el-row type="flex" justify="end">
                  <!-- 两个内容 -->
                  <el-col>{{ data.manager }}</el-col>
                  <!-- 下拉菜单 element -->
                  <el-col>
                    <el-dropdown>
                      <span> 操作<i class="el-icon-arrow-down" /> </span>
                      <el-dropdown-menu slot="dropdown">
                        <el-dropdown-item
                          @click.native="hAdd(data.id)"
                        >添加子部门</el-dropdown-item><el-dropdown-item
                          @click.native="hEdit(data.id)"
                        >编辑部门</el-dropdown-item><el-dropdown-item
                          v-if="data.children && data.children.length === 0"
                          @click.native="hDel(data.id)"
                        >删除部门</el-dropdown-item>
                      </el-dropdown-menu>
                    </el-dropdown>
                  </el-col>
                </el-row>
              </el-col>
            </el-row>
          </template>
        </el-tree>
      </el-card>

      <!-- 实现新增子部门或者编辑部门的对话框 -->
      <el-dialog
        :title="edit ? '编辑部门' : '新增部门'"
        :visible.sync="showDialog"
        :close-on-click-modal="false"
        :close-on-press-escape="false"
        @close="resetForm"
      >
        <!-- 在弹层中放入子组件 -->
        <!-- 解决数据不更新的问题: 方法一v-if="showDialog" 关闭和打开弹层 销毁和创建组件 -->
        <!-- 自定义组件里面了自定义了一个@close方法,用于控制弹窗的显示和隐藏 子传父的自定义事件,事件名close可以随意改-->
        <addOrEdit
          :id="curId"
          ref="addOrEditRef"
          :edit="edit"
          :origin-list="originList"
          @close="showDialog = false"
          @success="hSuccess"
        />
        <!-- <addOrEdit :id="curId" ref="addOrEditRef" :edit="edit" @success="hSuccess" /> -->
      </el-dialog>
    </div>
  </div>
</template>

<script>
// 引入新增子部门或者编辑部门的对话框的子组件
import addOrEdit from './deptDialog.vue'
import { getDepartments, delDepartment } from '@/api/departments'
import { transDataToTree } from '@/utils'
export default {
  components: { addOrEdit },
  data() {
    return {
      originList: [],
      // 定义变量,保存编辑状态
      edit: false,
      // 保存当前部门的id
      curId: '',
      // 控制子部门或者编辑部门的对话框的显示和隐藏
      showDialog: false,
      list: [], // 部门列表
      defaultProps: {
        children: 'children',
        label: 'label'
      }
    }
  },
  created() {
    // 重新渲染
    this.loadDepartments()
  },
  methods: {
    // 重置子组件中的表单
    resetForm() {
      // 通过在父组件中调用子组件中的重置方法resetForm实现重置表单功能
      this.$refs.addOrEditRef.resetForm()
    },
    // 删除部门
    async hDel(id) {
      this.$confirm('此操作将永久删除该部门, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(async() => {
          const { data: res } = await delDepartment(id)
          console.log('delDepartment', res)
          this.loadDepartments()
        })
        .catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          })
        })
    },
    // 编辑部门
    hEdit(id) {
      this.curId = id
      this.showDialog = true
      this.edit = true
      // dom更新是异步的
      this.$nextTick(() => {
        // 使用子组件调用
        // console.log('addOrEditRef', this.$refs.addOrEditRef)
        this.$refs.addOrEditRef.loadDepartmentDetails()
      })
    },
    // 成功添加子部门
    hSuccess() {
      // 重新渲染
      this.loadDepartments()
      // 关闭弹窗
      this.showDialog = false
    },
    // 添加子部门获取id
    hAdd(id) {
      this.curId = id
      this.showDialog = true
      this.edit = false
    },
    // 获取部门列表
    async loadDepartments() {
      try {
        const { data: res } = await getDepartments()
        console.log('getDepartments', res)
        // 获取部门数据并取出,数组中每个对象的部门数据(i)
        this.originList = res.depts.map(item => {
          return { id: item.id, pid: item.pid, name: item.name, code: item.code }
        })
        // 优化 : 删除数组中的第一条数据,取出公司的名称
        res.depts.shift()
        // console.log('shift', res.depts.shift())
        // res.data
        this.list = transDataToTree(res.depts)
      } catch (error) {
        console.log('getDepartments错误')
      }
    }
  }
}
</script>
